//
//  ProductViewController.m
//  YunGou
//
//  Created by macbook on 16/5/19.
//  Copyright © 2016年 macbook. All rights reserved.
//

#import "ProductViewController.h"
#import "GoodsDetailViewController.h"
#import "ProductCell.h"
#import "ProductModel.h"
#import "NSObject+Tools.h"
#import <MJRefresh.h>

@interface ProductViewController ()
@property(nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,assign) NSInteger page;
@end

@implementation ProductViewController

static NSString * const reuseIdentifier = @"Cell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.collectionView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    layout.minimumLineSpacing = 1;
    layout.minimumInteritemSpacing = 0.5;
    layout.itemSize = CGSizeMake((ScreenWidth - 1)/2, 200);
    
    self.collectionView.collectionViewLayout = layout;
    
    _page = 0;
    _dataArray = [NSMutableArray array];
    [self loadData];
    
    self.collectionView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page ++;
        [self loadData];
    }];
    
    self.collectionView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self.collectionView.mj_header endRefreshing];
        _page = 0;
        _dataArray = [NSMutableArray array];
        [self loadData];
    }];
}

- (void)loadData{
    [MBProgressHUD showMessage:@"正在加载"];
    if (self.type == ShanDuo) {
        [NetWorkTool shanDuoWithStart:_page * 10 success:^(id object) {
            [MBProgressHUD hideHUD];
            if ([object[@"code"] integerValue] == 1) {
                for (NSDictionary *dict in object[@"data"]) {
                    ProductModel *productModel = [ProductModel modelWithDict:dict];
                    [_dataArray addObject:productModel];
                }
            }
            [self.collectionView reloadData];
            [self.collectionView.mj_footer endRefreshing];
        } failure:^(NSError *error) {
            
        }];
    }else if(self.type == QiaoDuo){
        [NetWorkTool qiaoDuoWithStart:_page * 10 success:^(id object) {
            [MBProgressHUD hideHUD];
            if ([object[@"code"] integerValue] == 1) {
                for (NSDictionary *dict in object[@"data"]) {
                    ProductModel *productModel = [ProductModel modelWithDict:dict];
                    [_dataArray addObject:productModel];
                }
            }
            [self.collectionView reloadData];
            [self.collectionView.mj_footer endRefreshing];
        } failure:^(NSError *error) {
            
        }];
    }else if(self.type == NewProduct){
        [NetWorkTool newProductsWithStart:_page * 10 success:^(id object) {
            [MBProgressHUD hideHUD];
            if ([object[@"code"] integerValue] == 1) {
                for (NSDictionary *dict in object[@"data"]) {
                    ProductModel *productModel = [ProductModel modelWithDict:dict];
                    [_dataArray addObject:productModel];
                }
            }
            [self.collectionView reloadData];
            [self.collectionView.mj_footer endRefreshing];
        } failure:^(NSError *error) {
        }];
    }else{
        [NetWorkTool hotRecommendWithStart:_page * 10 success:^(id object) {
            [MBProgressHUD hideHUD];
            if ([object[@"code"] integerValue] == 1) {
                for (NSDictionary *dict in object[@"data"]) {
                    ProductModel *productModel = [ProductModel modelWithDict:dict];
                    [_dataArray addObject:productModel];
                }
            }
            [self.collectionView reloadData];
            [self.collectionView.mj_footer endRefreshing];
        } failure:^(NSError *error) {
            
        }];
    }
}

#pragma mark <UICollectionViewDataSource>
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    ProductCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    ProductModel *productModel = _dataArray[indexPath.item];
    cell.productModel = productModel;
    cell.block = ^{
        [NetWorkTool addShopCartWithGoodsId:productModel.Id success:^(id object) {
            if ([object[@"code"] integerValue] == 1) {
                [MBProgressHUD showSuccess:@"添加成功"];
            }
        } failure:^(NSError *error) {
        }];
    };
    
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GoodsDetailViewController *goodsDetailVC = SB(@"Common", @"GoodsDetailVC");
    ProductModel *productModel = _dataArray[indexPath.item];
    goodsDetailVC.productModel = productModel;
    goodsDetailVC.detailType = DonotAnnounce;
    [self.navigationController pushViewController:goodsDetailVC animated:YES];
}
@end
